const mysql = require("mysql")
const express = require("express")()
const static =  require("express-static")
const ejs = require("ejs")
const url = require("url")
const port = 8087




var sql = mysql.createConnection({
	database:"chuanyue",
	host:"localhost",
	user:"root",
	password:"10086"
})

sql.connect();
console.log(`数据链接成功`)


//配置首页
express.get("/",(request,response)=>{
	ejs.renderFile("dataa/shouye.html",{},(err,data)=>{
		if(err){
			console.log(err);
			response.end("请求失败")
		}
		else{
			response.end(data)
		}
	})
})
	

//列表页
express.get("/jiejuefanganliebiao",(request,response)=>{

	sql.query("select * from jiejuefangan",(err,data)=>{
		if(err){
			console.log(err);
			console.log("查询错误");
			response.end("请求失败")
		}
		else{
			ejs.renderFile("dataa/jiejuefanganliebiao.html",{list:data},(err,result)=>{
				if(err){
					console.log(err)
				}
				else{
					response.end(result)
				}
			})
		}
	})	
})



//详情页
express.get("/jiejuefanganxiangqing",(request,response)=>{
	const id = url.parse(request.url,true).query.id;
	sql.query(`select * from jiejuefangan where id=${id}`,(err,data)=>{
		if(err){
			console.log(err);
			console.log("查询错误");
			response.end("请求失败")
		}
		else{
			ejs.renderFile("dataa/jiejuefanganxiangqing.html",{data:data[0]},(err,result)=>{
				if(err){
					console.log(err)
				}
				else{
					response.end(result)
				}
			})
		}
	})	
})

express.get("/admin",(request,response)=>{

	sql.query(`select * from jiejuefangan`,(err,data)=>{
		if(err){
			console.log(err);
			response.end("请求失败")
		}
		else{
			ejs.renderFile("dataa/admin/delete.html",{data:data},(err,result)=>{
				if(err){
					console.log(err)
				}
				else{
					response.end(result)
					console.log("检测到访问请求")
				}
			})
		}
	})
})


//删除文章
express.get("/deletePost",(request,response)=>{
	const id = url.parse(request.url,true).query.id;
	if(!id){
		response.end("err")
		console.log("删除失败,未检测到前端的操作ID")
	}
	else{
		sql.query(`delete from jiejuefangan where id=${id}`,(err,result)=>{
			if(err){
				response.end("err")
				console.log(err)
			}

			else{
				response.end("come on")
				console.log(`${id}已被删除`)
			}

		})
	}
})


//查找新闻
express.get("/findPost",(request,response)=>{

	const id = url.parse(request.url,true).query.id;
	sql.query(`select * from jiejuefangan where id=${id}`,(err,data)=>{
		if(err){
			console.log(err)
			console.log("数据库查询错误");
			response.end("数据库出错");
		}
		else{
			response.end(JSON.stringify(data))
		}
	})
})

// 修改新闻
express.get("/amendPost",(request,response)=>{
	var arg = url.parse(request.url,true).query;
	const id = arg.id;
	const biaoti = arg.biaoti;
	const biaoqian = arg.biaoqian;
	const zhengwen = arg.zhengwen;


	sql.query(`update jiejuefangan set biaoti="${biaoti}",zhengwen="${zhengwen}",biaoqian="${biaoqian}" where id=${id}`,(err,data)=>{
		if(err){
			console.log(err)
			console.log("数据库查询错误");
			response.end("数据库出错");
		}
		else{
			response.end("success")
		}
	})
})



//配置静态请求路径
express.use(static(__dirname + "/dataa"))
express.listen(port)
console.log(`port is running ${port}`)